/*
 * Step.java
 *
 * Created on February 2, 2007, 8:15 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.smallx.xpath.streaming;

import com.smallx.xpath.DynamicContext;
import java.util.ArrayList;
import java.util.List;
import org.infoset.xml.Item;
import org.infoset.xml.ItemDestination;
import org.infoset.xml.XMLException;
import org.infoset.xml.filter.ItemFilter;

/**
 *
 * @author alex
 */
public abstract class Step implements ItemFilter {

   protected Step parent;
   protected ItemDestination nextStep;
   protected List<Predicate> predicates;
   
   /** Creates a new instance of Step */
   public Step() {
      this.parent = null;
      this.predicates = null;
   }
   public Step(Step parent) {
      this.parent = parent;
      this.predicates = null;
   }
   
   public void addPredicate(Predicate p) {
      if (this.predicates==null) {
         this.predicates = new ArrayList<Predicate>();
      }
      this.predicates.add(p);
   }
   
   public boolean testPredicates(Item item,DynamicContext context) {
      if (predicates!=null) {
         for (Predicate p : predicates) {
            if (!p.test(item,context)) {
               return false;
            }
         }
      }
      return true;
   }
   
   public abstract void send(Item item)
      throws XMLException;
   
   protected void notMatching() {
      if (parent!=null) {
         parent.notifyNotMatching();
      }
   }
   
   public void notifyNotMatching() {
   }
   
   protected void post(Item item) 
      throws XMLException
   {
      if (nextStep!=null) {
         nextStep.send(item);
      }
   }
   
   public void attach(ItemDestination dest) {
      nextStep = dest;
   }
   
}
